package stat;

import java.util.ArrayList;
import java.util.Map;

public class SuccessRatio {
	/**
	 * @param args
	 */
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
//		String file = "scripts/gnuplot/logs/stat_rarest_first.log";
//		String file = "scripts/gnuplot/logs/stat_sliding_window.log";
		String file = "scripts/gnuplot/logs/stat_segments.log";

		//read log
		Map map = LogReader.getMap(file);

		//number of peers
		int nrPeers = map.size();

		//size of time array
		long firstTimeValue = 0;
		int size = 0;
		boolean firstTime = false;
		for(Object peer : map.keySet()) { //each peer
			ArrayList<LogReaderLine> lines = (ArrayList<LogReaderLine>) map.get((String)peer);
			for(LogReaderLine lrl : lines) {
				size++;

				if(!firstTime) {
					firstTimeValue = lrl.getTimestamp();
				}

				firstTime = true;
			}
			break;
		}

		//time array
		long[] time = new long[size];

		//calculate X;Y sets block arrival times
		for(Object peer : map.keySet()) { //each peer
			ArrayList<LogReaderLine> lines = (ArrayList<LogReaderLine>) map.get((String)peer);
			int i = 0;
			for(LogReaderLine lrl : lines) {
				time[i] += lrl.getTimestamp();
				i++;
			}
		}

		int chunks = 0;
		for(long val : time) {
			float seconds = ((val/nrPeers)-firstTimeValue)/1000;
			
			if(seconds <= 60) {
				chunks++;
			}
		}
		
		float res = ((float)chunks/(float)size)*100;
		System.out.println("Success ratio: " + res);
	}
}
